-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use PSR-6 caching interfce & remove desarolla2 cache #10
Conversation
sunspikes
commented
Oct 27, 2017
- Use PSR-6 caching interfaces
- Remove Dsarrola cache adapter and use php-cache (for functional tests)
ad84c44
to
fcf7df7
Compare
Hi @Feijs Can you please have a look. So basically i removed Desarrola completely and decoupled the caching part (it now depends only on PSR-6 interfaces), for functional tests I'm using the array cache from php-cache and I've started using PHP 7 typehints. Now for v2 i think,
That's what i can think of now, what you think ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please have a look. So basically i removed Desarrola completely and decoupled the caching part (it now depends only on PSR-6 interfaces), for functional tests I'm using the array cache from php-cache and I've started using PHP 7 typehints.
Looks good! 👍 I've added a few (mostly minor) notes
Add back unit tests for throttling algorithms
Why have these been removed? I can update them to match your changes in this PR if that is the issue?
- Need to enforce strict types and type hints in all places (I think nullable typehints and void types would have been better, but for this min version need to be bumped to 7.1)
- Refactor the throttling algorithms a bit to reduce duplicate code & improve the documentation a bit more
Agreed, perhaps an incremental upgrade to 7.1 would be best, but since active support for 7.0 ends in a month, it it quite reasonable to update to 7.1 in one go
build/logs/clover.xml
Outdated
@@ -0,0 +1,3294 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think this should have been submitted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch!, removed, thanks!
src/Cache/ThrottlerCache.php
Outdated
*/ | ||
public function getItem(string $key): ThrottlerItemInterface | ||
{ | ||
$item = $this->getThrottlerItem($key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be returned inline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/Cache/ThrottlerCache.php
Outdated
} | ||
|
||
/** | ||
* @param string $key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docblocks (for the public methods) can all be replaced by @inheritdoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/Cache/ThrottlerCache.php
Outdated
* @throws CacheAdapterException | ||
* @throws ItemNotFoundException | ||
*/ | ||
private function getThrottlerItem(string $key): ThrottlerItemInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both private methods are used only once, so could be moved to the corresponding public method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks!
src/Cache/ThrottlerCache.php
Outdated
/** | ||
* @param string $key | ||
* | ||
* @return ThrottlerItemInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add these exceptions to the interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense, thanks!
src/Cache/ThrottlerCache.php
Outdated
$cacheItem = $this->cacheItemPool->getItem($key); | ||
|
||
if ($cacheItem->isHit()) { | ||
$this->cacheItemPool->deleteItem($key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the item deleted if it already exists? Is there an issue with updates if the item pre-existed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, when i tested (with redis) i see the item gets duplicated. this way it works, but may be we can add more real tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so i got the problem, it was because all tests were using same key. I've fixed this issue and I removed the delete line. And now the tests are running on redis by default (if available)
src/Throttle/Entity/CacheCount.php
Outdated
/** @var int $count */ | ||
private $count; | ||
|
||
/** @var int $ttl */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ttl
can also be null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -101,7 +104,9 @@ public function count() | |||
} | |||
|
|||
try { | |||
$this->counter = $this->cache->get($this->key); | |||
/** @var CacheCount $item */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a nice improvement to check whether these items contain the expected type:
if (!$item instanceof CacheCount) {
$this->counter = 0;
}
Theoretically the cache could contain garbage or unsafe data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps something for a seperate PR though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this can be done, but in case it is a hit, it should get the actual saved value (best case)
|
||
$this->cache->set($this->key, $this->counter, $this->ttl); | ||
$this->cache->setItem($this->key, $item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The item
variable is used only once, so this can be done inline. Applies to a number of similar cases below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -46,6 +44,7 @@ public function testThrottlePostLimit() | |||
$throttle->hit(); | |||
} | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
superfluous newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
It would be awesome if you can add it back. Now all of them fails because of these new changes, i've tried to fix it but it was not good as i wanted it to be :D @Feijs Also, thank you so much for your comments! |
f780b9b
to
8bf2713
Compare
d22b9d5
to
6901f96
Compare
16358df
to
1e9dfb7
Compare
I will merge this to the release branch, if there is something to be addressed it can be done in a new PR. |